<?php
namespace Admin\Controller;

use Think\Controller;

class AdminController extends CommenController
{
    /**
     * 角色列表
     */
    public function role_list()
    {
        $info = M('merchant_role', '', 'XYC');
        $list = $info->WHERE(['status' => '1','version'=>2])->SELECT();
        foreach ($list as $k => $v) {
            $list[$k]['ctime'] = date('Y-m-d H:i:s', $v['ctime']);
        }
        $this->assign(array(
            'list' => $list
        ));
        $this->display();
    }

    /**
     * 添加角色并分配权限页面
     */
    public function addrole()
    {
        $info = M('merchant_power', '', 'XYC');
        $list = $info->WHERE("status=1 and version=2")->ORDER("weight asc")->SELECT();
        $list = get_new_arrays($list);
        $this->assign(array(
            'list' => $list
        ));
        $this->display();
    }

    /**
     * 添加角色操作
     */
    public function add_role()
    {
        $info = D('Role');
        if (!empty($_POST)) {
            $data['rolename'] = I('rolename');
            $data['top_id'] = I('top');
            $data['next_id'] = I('next');
            $data['last_id'] = I('last');
            $data['version'] = 2;
            $data['ctime'] = time();
            $res = $info->create($data, '1');
            if (!$res) {
                MycommonController::ajax('1002', $info->getError(), 'error');
            }
            $res = $info->add();
            if ($res) {
                MycommonController::ajax('1001', '添加成功');
            } else {
                MycommonController::ajax('1002', '添加失败', 'error');
            }
        }
    }

    /**
     * 修改角色权限页面
     */
    public function modify_power()
    {
        $info = M('merchant_power', '', 'XYC');
        $rid = I('get.rid');
        if (!empty($rid)) {
            $role = M('merchant_role', '', 'XYC')->WHERE(['id' => $rid])->FIND();
            $top = explode(',', $role['top_id']);
            $next = explode(',', $role['next_id']);
            $last = explode(',', $role['last_id']);
            $list = $info->WHERE(['status' => '1','version'=>2])->ORDER("weight asc")->SELECT();
            $list = get_new_arrays($list);
            $this->assign(array(
                'list' => $list,
                'role' => $role,
                'top' => $top,
                'next' => $next,
                'last' => $last
            ));
            $this->display();
        } else {
            $this->error('系统错误');
        }
    }

    /**
     * 修改角色权限操作
     */
    public function modify_role_power()
    {
        $info = D('Role');
        if (!empty($_POST)) {
            $rid = I('rid');
            $data['id'] = $rid;
            $data['rolename'] = I('rolename');
            $data['top_id'] = I('top');
            $data['next_id'] = I('next');
            $data['last_id'] = I('last');
            $res = $info->create($data, 2);
            if (!$res) {
                MycommonController::ajax('1002', $info->getError(), 'error');
            }
            $res = $info->WHERE(['id' => $rid])->SAVE();
            if ($res !== false) {
                MycommonController::ajax('1001', '修改成功');
            } else {
                MycommonController::ajax('1002', '修改失败', 'error');
            }
        }
    }

    /**
     * 删除角色操作
     */
    public function del_role()
    {
        $info = D('Role');
        if (!empty($_POST)) {
            $rid = I('rid');
//            $role = $info->WHERE(['id' => $rid])->FIND();
            $res = $info->WHERE(['id' => $rid])->DELETE();
            if ($res) {
                MycommonController::ajax('1001', '删除成功');
            } else {
                MycommonController::ajax('1002', '删除失败', 'error');
            }
        }
    }

}